using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mvLibrary;
using mvLibrary.Helpers.Utils;

namespace fenk.Admin.Bancos
{
    public partial class MBancos : mvLibrary.BaseForm
    {
        private const int CHEQUES_INDEX = 0;
        private const int DEBITOS_INDEX = 1;
        private const int DEPOSITOS_INDEX = 2;
        public MBancos()
        {
            InitializeComponent();
        }
        public override void AfterReadItem()
        {
            base.AfterReadItem();
            textFechaPago.Enabled = true;
            textFechaReg.Enabled = true;
            textImporte.Enabled = true;
            labelAviso.Visible = false;
            labelConciliacion.Text = "";
            //
            // Registros ya auditados o ya conciliados no pueden modificarse
            //
            if (UniItem.ExtractAsString(9) != "" || UniItem.ExtractAsString(10) != "" || UniItem.ExtractAsString(20) == "D" || UniItem.ExtractAsString(11)!="")
            {
                textFechaPago.Enabled = false;
                textFechaReg.Enabled = false;
                textImporte.Enabled = false;
                labelAviso.Visible = true;
                
            }
            if (UniItem.ExtractAsString(20) == "D")
            {
                labelConciliacion.Text = "Ya conciliado";
            }
        }
        public override void BeforeSaveItem()
        {
            //
            // Copiar codigo de usuario en altas
            //
            if (ItemStatus == Status.New)
            {
                UniItem.Replace(21, User);
            }
            //
            // Total $$$ detalle impo debe coincidir con $$$ importe 
            //
            if (UniItem.ExtractAsString(23) == "1")
            {
                if (textImporte.Text != labelTotalImpo.Text)
                {
                    CancelSaveItem = true;
                    _errorMessage = "Total importaciones no coincide con importe del movimiento";
                }
            }
            
        }

        private void mvCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (mvCheckBox1.Checked)
            {
                impoGrid.Enabled=true;
                impoGrid.Focus();
            }
            else
            {
                labelTotalImpo.Text = string.Empty;
                UniItem.Replace(24, string.Empty);
                UniItem.Replace(29, string.Empty);
                impoGrid.Rows.Clear();
                impoGrid.Enabled=false;
            }
        }


        protected override void OnCancelItem()
        {
            radCheques.Checked = true;
            this.cbTipoMovimiento.SelectedIndex = CHEQUES_INDEX;
            radDebitos.Checked = false;
            radDepositos.Checked = false;
        }


        private void radCheques_CheckedChanged(object sender, EventArgs e)
        {
            this.cbTipoMovimiento.SelectedIndex = CHEQUES_INDEX;
            Refactor(CHEQUES_INDEX);
        }

        private void radDebitos_CheckedChanged(object sender, EventArgs e)
        {
            this.cbTipoMovimiento.SelectedIndex = DEBITOS_INDEX;
            Refactor(DEBITOS_INDEX);
        }

        private void radDepositos_CheckedChanged(object sender, EventArgs e)
        {
            this.cbTipoMovimiento.SelectedIndex = DEPOSITOS_INDEX;
            Refactor(DEPOSITOS_INDEX);
        }

        private void Refactor(int selectedIndex)
        {
            BaseForm formulario = FindForm() as BaseForm;
            if (formulario == null)
            {
                return;
            }
            if (!Parent.Enabled)
            {
                return;// Evita validar el control cuando ItemSave() o ItemCancel() limpian el formulario
            }
            formulario.SetErrorProvider(this, "");
            if (selectedIndex == -1)
            {
                formulario.SetErrorProvider(this, "Seleccione un item de la lista");
                return;
            }

            formulario.SetItemId();

        }
        
    }
}

